package yxy.game.pm2.command.friend;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.Set;

/** 好友 申请列表 */
@MessageType(proto = Command.FriendApplicantList, classtype = FriendMessage.FriendApplicantListRequest.class)
public class CMD_FriendApplicantList extends Command<FriendMessage.FriendApplicantListRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendApplicantListRequest request) {
		String playerCode = messageSource.getTag();

		FriendMessage.FriendApplicantListResponse.Builder response = FriendMessage.FriendApplicantListResponse.newBuilder();

		FriendModule friendModule = FriendModule.get(playerCode);
		Set<String> all = friendModule.getApply();
		PlayerHandle playerHandle = new PlayerHandle();
		for (String applyCode : all) {
			PlayerBean playerBean = PlayerBean.get(applyCode);
			PlayerMessage.PlayerSimpleInfo.Builder builder = playerHandle.playerSimpleInfo(playerBean);
			builder.setOnline(playerBean.online());

			response.addApplicants(builder);
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, FriendMessage.FriendApplicantListResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}